const express = require('express')
const userinfoModel = require('../db/model/userinfoModel')
const md5 = require('md5')

let api = express.Router()


api.get('/login', (req, res) => {
    res.render('login.html')
})

api.get('/register', (req, res) => {
    res.render('register.html')
})


api.post('/register', (req, res) => {
    let { username, pwd } = req.body
    console.log(username);
    console.log(pwd);
    userinfoModel.findOne({ username }, (err, data) => {
        console.log(data);
        if (data) {
            res.send('被占用')
        } else {
            userinfoModel.create({ username, pwd: md5(pwd) }, (err, data) => {
                console.log(data);
                res.render('jump.html', { name: "注册成功" })

            })
        }
    })
})
api.post('/login', (req, res) => {
    let { username, pwd } = req.body

    userinfoModel.findOne({ username, pwd: md5(pwd) }, (err, data) => {


        if (data) {
            req.session.username = username
            res.render('jump.html', { name: "登陆成功" })
        } else {
            res.send('你的账号或者用户名输入有误')
        }
    })
})





module.exports = api